//
//  CookClassViewController.m
//  Products_B
//
//  Created by lanou on 16/5/28.
//  Copyright © 2016年 朋学友. All rights reserved.
//

#import "CookClassViewController.h"
#import "CookClassTableViewCell.h"
#import "RequestManager.h"
#import "CookClassModel.h"
#import "CookClassImageUrlCollectionViewCell.h"
#import "ClassVideoViewController.h"
#define kCookClassUrl @"http://api.izhangchu.com/?appVersion=4.4&sysVersion=9.3.2&devModel=iPhone"

@interface CookClassViewController () <UITableViewDataSource, UITableViewDelegate, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray *cookClassModelArray;

// 此变量用在下拉刷新和上拉加载中,page指的是起始页
@property (nonatomic, assign) NSInteger page;
@end

@implementation CookClassViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _page = 1;

    self.cookClassModelArray = [NSMutableArray array];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"食课";
    self.navigationController.navigationBar.translucent = NO;
    
    // 创建tableView
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kHeight - 50 - 64 - 70) style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.rowHeight = kWidth * 662 / 1242 + 100;
    [self.view addSubview:_tableView];
    
    // 注册自定义的cell
    [_tableView registerClass:[CookClassTableViewCell class] forCellReuseIdentifier:@"cookClassTableViewCell"];
    // 创建collectionView
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kWidth, 70) collectionViewLayout:flowLayout];
    // 设置全透明
    _collectionView.backgroundColor = [UIColor clearColor];
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    // 设置collectionView横向滑动
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    // 隐藏滑动条
    _collectionView.showsHorizontalScrollIndicator = NO;
 
    // 这张view用来作为tableView的表头，高度与collectionView一致，这加这个表头的目的是为了保证tableView的(0,0）点与collection的(0,0)点在同一位置的同时，tableView的顶部不会被collectionView覆盖
    self.view.backgroundColor = [UIColor clearColor];
    UIView *backGroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kWidth, 70)];
    backGroundView.backgroundColor = [UIColor clearColor];
    self.tableView.tableHeaderView = backGroundView;
    
    // 这张view是用来制造半透明效果
    UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kWidth, 70)];
    view2.backgroundColor = [UIColor whiteColor];
    view2.alpha = 0.8;
    [self.view addSubview:view2];
    
    [self.view addSubview:_collectionView];
    
    // 注册collectionViewCell
    [_collectionView registerClass:[CookClassImageUrlCollectionViewCell class] forCellWithReuseIdentifier:@"cookClassImageUrlCollectionViewCell"];
    
    // 调用网络请求的方法
    [self requestNetworkAboutCookClass];
    
    [self loadMoreData];
    [self updateData];
    
//    [self req];
}

// 网络请求,请求食课界面的数据
- (void)requestNetworkAboutCookClass {
    // NSDictionary *parDic = @{@"methodName":@"CourseIndex",@"page":@(_page),@"size":@10,@"token":@"520B46DBD3C835733C9E7D2E345754A9",@"user_id":@"1513438",@"version":@4.4};
    NSDictionary *parDic = @{@"methodName":@"CourseIndex",@"page":@(_page),@"size":@10,@"token":@"520B46DBD3C835733C9E7D2E345754A9",@"version":@4.4};
    [RequestManager requestWithUrl:kCookClassUrl requestType:RequestTypePOST parDic:parDic finish:^(NSData *data) {
        
        NSDictionary *jsDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSLog(@" -=-= %@",jsDic);
        // 模型数组
        NSMutableArray *tempModelArray = [CookClassModel cookClassModelAnalyseDataWithJsonDic:jsDic];
        [self.cookClassModelArray addObjectsFromArray:tempModelArray];
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
            [self.collectionView reloadData];
            
            [_tableView.mj_header endRefreshing];
            [_tableView.mj_footer endRefreshing];
        });
    } error:^(NSError *error) {
        NSLog(@"请求数据失败");
    }];
}


// 返回行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSLog(@" +++++++ %ld",_cookClassModelArray.count);
    return _cookClassModelArray.count;
}

// 返回cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CookClassTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cookClassTableViewCell" forIndexPath:indexPath];
    cell.cookClassModel = self.cookClassModelArray[indexPath.row];
    return cell;
}

// 点击cell的方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    ClassVideoViewController *classVideo = [[ClassVideoViewController alloc] init];
    CookClassModel *model = self.cookClassModelArray[indexPath.row];
    // 属性传值
    classVideo.ralateId = model.series_id;
    
    // 更新的集数
    classVideo.upDateToClassNumber = model.episode;
    
    [self.navigationController pushViewController:classVideo animated:YES];
}

// 返回collectionView的item个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.cookClassModelArray.count;
}

// 返回collectionView的cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    CookClassImageUrlCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cookClassImageUrlCollectionViewCell" forIndexPath:indexPath];
    CookClassModel *cookClassModel = self.cookClassModelArray[indexPath.row];
    [cell.scrollImageView sd_setImageWithURL:[NSURL URLWithString:cookClassModel.album_logo]];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    ClassVideoViewController *classVideo = [[ClassVideoViewController alloc] init];
    CookClassModel *model = self.cookClassModelArray[indexPath.row];
    // 属性传值
    classVideo.ralateId = model.series_id;
    
    // 更新的集数
    classVideo.upDateToClassNumber = model.episode;
    
    [self.navigationController pushViewController:classVideo animated:YES];
}

// 上拉加载更多
- (void)loadMoreData{
        self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            _page += 1;
            [self requestNetworkAboutCookClass];
        }];
 
}

// 下拉刷新
- (void)updateData{
        self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            _page = 1;
            [self.cookClassModelArray removeAllObjects];
            [self requestNetworkAboutCookClass];
        }];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
